package com.example.fillthemapwithtubes.view;

import java.util.List;

import com.example.fillthemapwithtubes.R;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListDialog extends Dialog {

	public interface OnItemClickListener {
		void onItemClick(AdapterView<?> parent, View view, int position, long id);
	}

	private Context context;
	private ListView listView;
	private List<?> tagList;
	private List<String> textList;
	private OnItemClickListener onItemClickListener;

	public ListDialog(Context context, String title, List<String> textList, List<?> tagList,
			OnItemClickListener onItemClickListener) {
		super(context);
		this.context = context;
		this.textList = textList;
		this.tagList = tagList;
		this.onItemClickListener = onItemClickListener;
		this.setTitle(title);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initDialog();
	}

	private void initDialog() {
		this.setContentView(R.layout.dialog_record_map);
		listView = (ListView) this.findViewById(R.id.listView_data);
		TagListAdapter adapter = new TagListAdapter(context, android.R.layout.select_dialog_item);
		adapter.addAll(textList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (null != onItemClickListener) {
					onItemClickListener.onItemClick(parent, view, position, id);
				}
				ListDialog.this.dismiss();
			}
		});
	}

	private class TagListAdapter extends ArrayAdapter<String> {

		public TagListAdapter(Context context, int resource) {
			super(context, resource);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = super.getView(position, convertView, parent);
			v.setTag(tagList.get(position));
			return v;
		}
	}
}
